'''
Created on 2013-01-04

@author: yangzhao
'''

# Create your models here.
from django.db import models
from django.contrib import admin
from django.core.validators import MinValueValidator

#model of platform
class platform(models.Model): 
    name = models.CharField(max_length=30)
    url = models.URLField(max_length=200,verify_exists=True)
    status = models.IntegerField(max_length=4)
    order = models.IntegerField(max_length=4,validators=[MinValueValidator(0)],unique=True)
    
    class Meta:
        ordering= ['order']
        
    def __unicode__(self):
        return self.name
    
#admin of platform    
class platformAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['name']}),
               (None, {'fields':['url']}),
               (None, {'fields':['status']}),
               (None, {'fields':['order']}),
               ]
    list_display = ('name','url','status', 'order')
    search_fields = ['name','url']

admin.site.register(platform,platformAdmin)

#model of level
class level(models.Model):
    level = models.IntegerField(max_length=4,validators=[MinValueValidator(0)],unique=True)
    name = models.CharField(max_length=30,null=True,blank=True)
    criterion = models.TextField(max_length=200,null=True,blank=True)
    privilege = models.TextField(max_length=200,null=True,blank=True)
    
    class Meta:
        ordering= ['level']
        
    def __unicode__(self):
        return self.level
    
#admin of level
class levelAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['level']}),
               (None, {'fields':['name']}),
               (None, {'fields':['criterion']}),
               (None, {'fields':['privilege']}),
               ]
    list_display = ('level','name')
    search_fields = ['level','name']

admin.site.register(level,levelAdmin)

      
#model of user
class user(models.Model): 
    GENDER = (
    (u'M', u'Male'),
    (u'F', u'Female'), 
     )
    account = models.CharField(max_length=30)
    platform = models.ForeignKey(platform)
    nickname = models.CharField(max_length=30)
    gender = models.CharField(max_length=8, choices=GENDER)
    level = models.ForeignKey(level)
    points = models.IntegerField(max_length=4,validators=[MinValueValidator(0)])
    is_disable = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True,null=True,blank=True)
    is_talent = models.BooleanField(default=False,null=True,blank=True)
    is_member = models.BooleanField(default=False,null=True,blank=True)
    is_vip = models.BooleanField(default=False,null=True,blank=True)
    open_days = models.IntegerField(max_length=4,validators=[MinValueValidator(0)],null=True,blank=True)

#admin of user    
class userAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['account']}),
               (None, {'fields':['platform']}),
               (None, {'fields':['nickname']}),
               (None, {'fields':['level']}),
               (None, {'fields':['points']}),
               (None, {'fields':['is_disable']}),
               ]
    list_display = ('account','platform','nickname', 'level',)
    search_fields = ['account','platform','nickname','level']

admin.site.register(user,userAdmin)

#model of login_records
class loginRecords(models.Model):
    user = models.ForeignKey(user)
    login_time = models.DateTimeField()
    logout_time = models.DateTimeField()
    login_ip = models.IPAddressField()
    login_browser = models.CharField(max_length=30,null=True,blank=True)
    
    class Meta:
        ordering= ['logout_time']
        
    def __unicode__(self):
        return self.login_ip

#admin of loginRecords    
class loginRecordsAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['user']}),
               (None, {'fields':['login_time']}),
               (None, {'fields':['logout_time']}),
               (None, {'fields':['login_ip']}),
               (None, {'fields':['login_browser']}),
               ]
    list_display = ('user','login_time','logout_time', 'login_ip','login_browser')
    search_fields = ['user','login_ip','login_browser']

admin.site.register(loginRecords,loginRecordsAdmin)


#models of catalog
class catalog(models.Model):
    name = models.CharField(max_length=10)
    order = models.IntegerField(max_length=4,validators=[MinValueValidator(0)],unique=True)
    class Meta:
        ordering= ['order']
        
    def __unicode__(self):
        return self.name
    
#admin of catalog    
class catalogAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['name']}),
               (None, {'fields':['order']}),
               ]
    list_display = ('name','order')
    search_fields = ['name','order']

admin.site.register(catalog,catalogAdmin)

#models of forwardingRequest
class forwardingRequest(models.Model):
    pub_user = models.ForeignKey(user)
    message = models.CharField(max_length=140)
    
    class Meta:
        ordering= ['pub_user']

#model of followRequest
class followRequest(models.Model):
    pub_user = models.ForeignKey(user)
    
    class Meta:
        ordering= ['pub_user']


#model of projectRequest
class projectRequest(models.Model):
    name = models.CharField(max_length=30)
    pub_user = models.ForeignKey(user)
    url = models.URLField()
    description = models.TextField(max_length=200,null=True,blank=True)
    logo = models.ImageField(null=True,blank=True)
    
    class Meta:
        ordering= ['pub_user']
    
    
#model of comments
class comment(models.Model):
    user = models.ForeignKey(user)
    comment = models.TextField(max_length=140)
    class Meta:
        ordering= ['user']
        
#admin of comments    
class commentAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['user']}),
               (None, {'fields':['comment']}),
               ]
    list_display = ('user','comment')
    search_fields = ['user']

admin.site.register(comment,commentAdmin)  

#model of allocation
class allocation(models.Model):
    name = models.CharField(max_length=30)
    forwarding = models.ForeignKey(forwardingRequest,null=True,blank=True)
    follow = models.ForeignKey(followRequest,null=True,blank=True)
    project = models.ForeignKey(projectRequest,null=True,blank=True)
    catalog = models.ForeignKey(catalog)
    reward = models.IntegerField(max_length=4,validators=[MinValueValidator(0)])
    expire_time = models.DateTimeField()
    expire_points = models.IntegerField(max_length=8,validators=[MinValueValidator(0)])# set 0 means no limit
    comment = models.ForeignKey(comment,null=True,blank=True)
    start_numFans = models.IntegerField(max_length=4,validators=[MinValueValidator(0)],null=True,blank=True)
    end_numFans = models.IntegerField(max_length=4,validators=[MinValueValidator(0)],null=True,blank=True)
    is_talent = models.BooleanField(default=False,null=True,blank=True)
    is_member = models.BooleanField(default=False,null=True,blank=True)
    is_vip = models.BooleanField(default=False,null=True,blank=True)
    open_days = models.IntegerField(max_length=4,validators=[MinValueValidator(0)],null=True,blank=True)
    is_photo = models.BooleanField(default=False,null=True,blank=True)
    
    class Meta:
        ordering= ['name']
        
    def __unicode__(self):
        return self.name

#admin of allocation    
class allocationAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['name']}),
               ('Target', {'classes': ('collapse',),'fields':['follow','project','forwarding']}),
               (None, {'fields':['catalog']}),
               (None, {'fields':['reward']}),
               ('Limiting options', {'classes': ('collapse',),'fields':['expire_time','expire_points','start_numFans','end_numFans']}),
               ('Comments options', {'fields':['comment']}),
               ('Advanced options', {'classes': ('collapse',),
                                     'fields':['is_talent','is_member','is_vip','is_photo','open_days']}),
               ]
    list_display = ('name','forwarding','catalog','expire_time','expire_points','start_numFans','end_numFans','reward','comment','is_talent','is_member','is_vip','open_days','is_photo')
    search_fields = ['name','catalog']

admin.site.register(allocation,allocationAdmin)   


class allocationInline(admin.StackedInline):
    model = allocation
    extra = 3
    
#admin of forwardingRequest    
class forwardingRequestAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['pub_user']}),
               (None, {'fields':['comment']}),
               ]
    inlines = [allocationInline]
    list_display = ('pub_user','comment')
    search_fields = ['pub_user']

admin.site.register(forwardingRequest,forwardingRequestAdmin)

   
#admin of followRequest 
class followRequestAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['pub_user']}),
               ]
    inlines = [allocationInline]
    list_display = ('pub_user')
    search_fields = ['pub_user']

admin.site.register(followRequest,followRequestAdmin)
    

#admin of projectRequest 
class projectRequestAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['name']}),
               (None, {'fields':['pub_user']}),
               (None, {'fields':['url']}),
               (None, {'fields':['description']}),
               (None, {'fields':['logo']}),
               ]
    inlines = [allocationInline]
    list_display = ('name','pub_user','url','description','logo')
    search_fields = ['name','pub_user','url']

admin.site.register(projectRequest,projectRequestAdmin)

#model of inviteRecord
class inviteRecord(models.Model):
    inviter = models.ForeignKey(user, related_name='+')
    be_invited_user = models.ForeignKey(user, related_name='+')
    be_invited_time = models.DateTimeField()
    
    class Meta:
        ordering= ['be_invited_time']
        
  
#doTask(forwad, follow, project)


    
#admin of inviteRecord 
class inviteRecordAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['inviter']}),
               (None, {'fields':['be_invited_user']}),
               (None, {'fields':['be_invited_time']}),
               ]
    list_display = ('inviter','be_invited_user','be_invited_time')
    search_fields = ['inviter','be_invited_user']

admin.site.register(inviteRecord,inviteRecordAdmin)

#model of typeOfPayment
class typeOfPayment(models.Model):
    STATUS = (
              (u'N', u'Normal'),
              (u'D', u'Disconnected'), 
              (u'T', u'Test'),
     )
    name = models.CharField(max_length=30)
    url = models.URLField()
    status = models.CharField(choice=STATUS)
    
    class Meta:
        ordering= ['name']
        
    def __unicode__(self):
        return self.name

#admin of typeOfPayment
class typeOfPaymentAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['name']}),
               (None, {'fields':['url']}),
               (None, {'fields':['status']}),
               ]
    list_display = ('name','url','status')
    search_fields = ['inviter','url']

admin.site.register(typeOfPayment,typeOfPaymentAdmin)

#model of rechargeSet
class rechargeSet(models.Model):
    name = models.CharField(max_length=30)
    amount = models.IntegerField(max_length=8,validators=[MinValueValidator(0)])
    point = models.IntegerField(max_length=10,validators=[MinValueValidator(0)])
    description = models.CharField(max_length=140,null=True,blank=True)
    start_date = models.DateField(null=True,blank=True)
    end_date =  models.DateField(null=True,blank=True)
    quantity = models.IntegerField(max_length=8,validators=[MinValueValidator(0)],null=True,blank=True)
    level_limit = models.ForeignKey(level,null=True,blank=True)
    
    class Meta:
        ordering= ['name']
        
    def __unicode__(self):
        return self.name
    
    
#admin of rechargeSet
class rechargeSetAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['name']}),
               (None, {'fields':['amount']}),
               (None, {'fields':['point']}),
               (None, {'fields':['description']}),
               (None, {'fields':['start_date']}),
               (None, {'fields':['end_date']}),
               (None, {'fields':['quantity']}),
               (None, {'fields':['level_limit']}),
               ]
    list_display = ('name','amount','point','start_date','end_date','quantity','level_limit')
    search_fields = ['name']

admin.site.register(rechargeSet,rechargeSetAdmin)    

#model of rechargeRecord
class rechargeRecord(models.Model):
    user = models.ForeignKey(user)
    type_of_payment = models.ForeignKey(typeOfPayment)
    recharge_Set = models.ForeignKey(rechargeSet)
    payment_time = models.DateTimeField()
    payment_order = models.CharField(max_length=30)
    payment_status = models.CharField(max_length=8)
    
    class Meta:
        ordering= ['user']

#admin of rechargeRecord
class rechargeRecordAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['user']}),
               (None, {'fields':['type_of_payment']}),
               (None, {'fields':['recharge_Set']}),
               (None, {'fields':['payment_time']}),
               (None, {'fields':['payment_order']}),
               (None, {'fields':['payment_status']}),
               ]
    list_display = ('user','type_of_payment','recharge_Set','payment_time','payment_order','payment_status')
    search_fields = ['user','type_of_payment','recharge_Set','payment_order']

admin.site.register(rechargeRecord,rechargeRecordAdmin)   
    


#withdrawalsRecord
class withdrawalsRecord(models.Model):
    STATUS = (
              (u'P', u'Processing'),
              (u'C', u'Cancel'), 
              (u'S', u'Succeed'),
     )
    user = models.ForeignKey(user)
    amount = models.IntegerField(max_length=8,validators=[MinValueValidator(0)])
    account = models.CharField(max_length=18)
    type_of_payment = models.ForeignKey(typeOfPayment)
    real_name = models.CharField(max_length=18)
    withdraw_time = models.DateTimeField()
    withdraw_status = models.CharField(choice=STATUS)
    
    class Meta:
        ordering= ['withdraw_time']

#admin of withdrawalsRecord
class withdrawalsRecordAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['user']}),
               (None, {'fields':['amount']}),
               (None, {'fields':['account']}),
               (None, {'fields':['type_of_payment']}),
               (None, {'fields':['real_name']}),
               (None, {'fields':['withdraw_time']}),
               (None, {'fields':['withdraw_status']}),
               ]
    list_display = ('user','amount','account','type_of_payment','real_name','withdraw_time','withdraw_status')
    search_fields = ['user','real_name',]

admin.site.register(withdrawalsRecord,withdrawalsRecordAdmin) 

#rank



#model of question
class question(models.Model):
    question = models.CharField(max_length=30)
    answer = models.CharField(max_length=180)
    pub_time = models.DateTimeField()
    order = models.IntegerField(max_length=4,validators=[MinValueValidator(0)],unique=True)
    class Meta:
        ordering= ['order']
        
#admin of question
class questionAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['question']}),
               (None, {'fields':['answer']}),
               (None, {'fields':['pub_time']}),
               (None, {'fields':['order']}),
               ]
    list_display = ('question','answer','pub_time','order')
    search_fields = ['question','answer']

admin.site.register(question,questionAdmin) 

#model of about
class about(models.Model):
    title = models.CharField(max_length=180)
    content = models.TextField()
    pub_time = models.DateTimeField()
    
    class Meta:
        ordering= ['pub_time']
        
#admin of about
class aboutAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['title']}),
               (None, {'fields':['content']}),
               (None, {'fields':['pub_time']}),
               ]
    list_display = ('title','pub_time')
    search_fields = ['title']

admin.site.register(about,aboutAdmin) 


#model of contact
class contact(models.Model):
    user = models.ForeignKey(user)
    email = models.EmailField()
    title = models.CharField(max_length=30)
    content = models.TextField()
    send_time = models.DateTimeField()
    
    class Meta:
        ordering= ['pub_time']
        
#admin of contact
class contactAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['user']}),
               (None, {'fields':['email']}),
               (None, {'fields':['title']}),
               (None, {'fields':['content']}),
               (None, {'fields':['send_time']}),
               ]
    list_display = ('user','email','title','send_time')
    search_fields = ['user','email','title','send_time']

admin.site.register(contact,contactAdmin) 

#model of advert
class advert(models.Model):
    name = models.CharField(max_length=30)
    position = models.CharField(max_length=30)
    code = models.TextField()
    add_time = models.DateTimeField()

    class Meta:
        ordering= ['add_time']
        
#admin of advert
class advertAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['name']}),
               (None, {'fields':['position']}),
               (None, {'fields':['code']}),
               (None, {'fields':['add_time']}),
               ]
    list_display = ('name','position','code','add_time')
    search_fields = ['name','position']

admin.site.register(advert,advertAdmin) 
     
#model of subtitle
class subtitle(models.Model):
    message = models.CharField(max_length=140)
    add_time = models.DateTimeField()

    class Meta:
        ordering= ['add_time']
        
#admin of subtitle
class subtitleAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['message']}),
               (None, {'fields':['add_time']}),
               ]
    list_display = ('message','add_time')
    search_fields = ['message']

admin.site.register(subtitle,subtitleAdmin) 


#model of title
class title(models.Model):
    message = models.CharField(max_length=140)
    add_time = models.DateTimeField()

    class Meta:
        ordering= ['add_time']
        
#admin of title
class titleAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['message']}),
               (None, {'fields':['add_time']}),
               ]
    list_display = ('message','add_time')
    search_fields = ['message']

admin.site.register(title,titleAdmin) 

#model of logo
class logo(models.Model):
    img = models.ImageField()
    text = models.CharField(max_length=140)
    add_time = models.DateTimeField()
    
    class Meta:
        ordering= ['add_time']
    
#admin of logo
class logoAdmin(admin.ModelAdmin):
    fieldsets=[
               (None, {'fields':['img']}),
               (None, {'fields':['text']}),
               (None, {'fields':['add_time']}),
               ]
    list_display = ('img','text','add_time')
    search_fields = ['text']

admin.site.register(logo,logoAdmin)     